package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;

public class CustomerServiceImpl implements CustomerService{
	private List<Customer> list=new ArrayList<Customer>();
	@Override
	public Customer addCustomer(Customer c) {
		synchronized (list) {
			c.setId(UUID.randomUUID().toString());
			list.add(c);
		}
		return c;
	}
	@Override
	public List<Customer> findAllCustomers() {
		return list;
	}
	@Override
	public Customer deleteCustomer(String id) {
		Customer c = null;
		c = FindCustomerById(id);
		if(c!=null){
			list.remove(c);
		}
		return c;
	}
	@Override
	public Customer FindCustomerById(String id) {
		Customer c = null;
		for(Customer customer:list){
			if(id.equals(customer.getId())){
				c=customer;
				break;
			}
		}
		return c;
	}
	@Override
	public Customer updateCustomer(Customer c) {
		int index = -1;
		for(int i=0;i<list.size();i++){
			if(c.getId().equals(list.get(i).getId())){
				index = i;
				break;
			}
		}
		if(index!=-1){
			list.set(index, c);
			
			return c;
		}
		return null;
	}
	

}
